package cn.source.web.controller.api;

import cn.source.common.constant.HttpStatus;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.domain.entity.SysUser;
import cn.source.common.core.domain.model.LoginUser;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.utils.DateUtils;
import cn.source.common.utils.SecurityUtils;
import cn.source.common.utils.StringUtils;
import cn.source.framework.web.service.TokenService;
import cn.source.system.domain.HouseEvaluate;
import cn.source.system.domain.HouseRoom;
import cn.source.system.domain.HouseUser;
import cn.source.system.domain.HouseVillage;
import cn.source.system.service.*;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/houseApi")
public class HouseApiController extends BaseController {
    @Resource
    private IHouseEvaluateService houseEvaluateService;

    @Resource
    private ISysUserService userService;

    @Resource
    private IHouseUserService houseUserService;

    @Resource
    private TokenService tokenService;

    @Resource
    private ISysDictTypeService dictTypeService;


    /**
     * 查询房源评价列表
     *
     * @param houseId
     * @return
     */
    @GetMapping("/selectHouseEvals")
    public TableDataInfo selectHouseEvals(Long houseId) {
        HouseEvaluate houseEvaluate = new HouseEvaluate();
        houseEvaluate.setHouseId(houseId);
        houseEvaluate.setState(1);//只查询显示的
        startPage();
        List<HouseEvaluate> list = houseEvaluateService.selectHouseEvaluateList(houseEvaluate);
        return getDataTable(list);
    }

    /**
     * 新增评价
     *
     * @param houseId
     * @param evalu
     * @return
     */
    @GetMapping("/saveHouseEvals")
    public AjaxResult saveHouseEvals(Long houseId, String evalu, Long userId) {
        HouseEvaluate houseEvaluate = new HouseEvaluate();
        houseEvaluate.setHouseId(houseId);
        houseEvaluate.setEvaluate(evalu);
        houseEvaluate.setState(1);//只查询显示的
        houseEvaluate.setCreateTime(DateUtils.getNowDate());
        houseEvaluate.setEvalUserId(userId);
        SysUser sysUser = userService.selectUserById(userId);
        houseEvaluate.setEvalUser(sysUser.getUserName());
        houseEvaluateService.insertHouseEvaluate(houseEvaluate);
        return AjaxResult.success(houseEvaluate);
    }

    /**
     * 评价鉴权，只有历史租客才能评价
     *
     * @param houseId
     * @param userId
     * @return
     */
    @GetMapping("/checkAuthEvals")
    public AjaxResult checkAuthEvals(Long houseId, Long userId) {
        HouseUser houseUser = new HouseUser();
        houseUser.setHouseId(houseId);
        houseUser.setUserId(userId);
        List<HouseUser> list = houseUserService.selectHouseUserList(houseUser);
        return AjaxResult.success(list);
    }


    @GetMapping("/getDictType")
    public AjaxResult getDictType(@RequestParam("dictType") String dictType){

        return AjaxResult.success(dictTypeService.selectDictDataByType(dictType));
    }
}
